package gateway.filters.pre;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public class SimpleFilter extends ZuulFilter
{

	private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);

	@Override
	public String filterType()
	{
		return "pre";
	}

	@Override
	public int filterOrder()
	{
		return 1;
	}

	@Override
	public boolean shouldFilter()
	{
		return true;
	}

	@Override
	public Object run()
	{
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();

		log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

		Object notPass = request.getParameter("not_pass");
		if (notPass == null)
		{
			log.warn("PASS is empty");
			ctx.setSendZuulResponse(false);
			ctx.setResponseStatusCode(401);
			ctx.setResponseBody(
				"{\"result\":\"through is empty and i am super man" + "! hashcode:" + log.hashCode() + "\"}");
			ctx.getResponse().setContentType("text/html;charset=UTF-8");
			return null;
		}
		log.info("PASS can be granted");

		return null;
	}

}
